/*
*	@file
*	@author  Thomas Kroes <t.kroes at tudelft.nl>
*	@version 1.0
*	
*	@section LICENSE
*	
*	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*	
*	Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*	Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*	Neither the name of the TU Delft nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
*	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include "vtkImageProcessingPass.h"
#include "vtkAmbientOcclusionPassDll.h"

#include <vtkImageProcessingPass.h>
#include <vtkSmartPointer.h>
#include <vtkUniformVariables.h>
#include <vtkImageNoiseSource.h>
#include <vtkgl.h>

class vtkOpenGLRenderWindow;
class vtkDepthPeelingPassLayerList; // Pimpl
class vtkShaderProgram2;
class vtkShader2;
class vtkFrameBufferObject;
class vtkTextureObject;

class VTK_AO_PASS_EXPORT vtkAmbientOcclusionPass : public vtkImageProcessingPass
{
public:
	static vtkAmbientOcclusionPass *New();
	vtkTypeMacro(vtkAmbientOcclusionPass,vtkImageProcessingPass);
	void PrintSelf(ostream& os, vtkIndent indent);

	//BTX
	virtual void PreRender(const vtkRenderState* RenderState);
	virtual void Render(const vtkRenderState* RenderState);
	//ETX

	void ReleaseGraphicsResources(vtkWindow* Window);

protected:
	// Description:
	// Default constructor. DelegatePass is set to NULL.
	vtkAmbientOcclusionPass();

	// Description:
	// Destructor.
	virtual ~vtkAmbientOcclusionPass();

	vtkSmartPointer<vtkFrameBufferObject>	FrameBufferObject;		// FBO
	vtkSmartPointer<vtkTextureObject>		RenderPass;				// Render pass
	vtkSmartPointer<vtkTextureObject>		DepthPass;				// Render pass
	vtkSmartPointer<vtkTextureObject>		NormalPass;				// Render pass
	vtkSmartPointer<vtkTextureObject>		SSAOTexture;			// Render pass
	vtkSmartPointer<vtkShaderProgram2>		Preprocess;				// Preprocessing shader
	vtkSmartPointer<vtkShaderProgram2>		SSAO;					// SSAO shader
	vtkSmartPointer<vtkShaderProgram2>		SSAOFilterH;			// SSAO horizontal filter shader
	vtkSmartPointer<vtkShaderProgram2>		SSAOFilterV;			// SSAO vertical filter shader
	vtkSmartPointer<vtkUniformVariables>	SSAOUniformVariables;	// SSAO shader uniforms
	vtkSmartPointer<vtkTextureObject>		RandomBuffer;	// Render pass
	int										WindowSize[2];			// Last render size
	bool									Supported;
	bool									SupportChecked;

private:
	vtkAmbientOcclusionPass(const vtkAmbientOcclusionPass&);  // Not implemented.
	void operator=(const vtkAmbientOcclusionPass&);  // Not implemented.
	void WriteRGBucTextureToDisk(vtkTextureObject* TextureObject, const char* Name);
	void WriteRGBAucTextureToDisk(vtkTextureObject* TextureObject, const char* Name);
	void WriteFloatTextureToDisk(vtkTextureObject* TextureObject, const char* Name);
	void LoadPreprocessingShader();
	void LoadSSAOShader();
	bool IsSupported(const vtkRenderState* RenderState);
};

/*
#include "vtkAmbientOcclusionPassDll.h"

#include <vtkImageProcessingPass.h>
#include <vtkSmartPointer.h>
#include <vtkUniformVariables.h>
#include <vtkgl.h>

class VTK_AO_PASS_EXPORT vtkAmbientOcclusionPass : public vtkImageProcessingPass
{
public:
	static vtkAmbientOcclusionPass *New();
	vtkTypeMacro(vtkAmbientOcclusionPass,vtkImageProcessingPass);
	void PrintSelf(ostream& os, vtkIndent indent);

	//BTX
	virtual void Render(const vtkRenderState *s);
	//ETX

	void ReleaseGraphicsResources(vtkWindow *w);

protected:
	vtkAmbientOcclusionPass();
	virtual ~vtkAmbientOcclusionPass();
	
	bool									Initialized;			// 
	vtkSmartPointer<vtkFrameBufferObject>	FrameBufferObject;		// 
	vtkSmartPointer<vtkTextureObject>		RenderPass;					// render target for the scene
	vtkSmartPointer<vtkTextureObject>		Result;					// render target for the scene
	vtkSmartPointer<vtkShaderProgram2>		Preprocess;				// Preprocessing shader
	vtkSmartPointer<vtkShaderProgram2>		SSAO;					// SSAO shader
	vtkSmartPointer<vtkShaderProgram2>		SSAOFilterH;			// SSAO horizontal filter shader
	vtkSmartPointer<vtkShaderProgram2>		SSAOFilterV;			// SSAO vertical filter shader
	vtkSmartPointer<vtkUniformVariables>	SSAOUniformVariables;	
	GLuint 									FBO;
	GLuint 									RandomUnitVectorsTexture;
	GLuint 									NormalBuffer;
	GLuint 									DepthBuffer;
	GLuint 									SSAOTexture;
	GLuint 									BlurTexture;

	bool Supported;
	bool SupportProbed;

private:
	vtkAmbientOcclusionPass(const vtkAmbientOcclusionPass&);	// Not implemented.
	void operator=(const vtkAmbientOcclusionPass&);				// Not implemented.
	void LoadPreprocessShaderProgram();
	void LoadSSAOShaderProgram();
	void LoadSSAOFilterHShaderProgram();
	void LoadSSAOFilterVShaderProgram();
	void CreateRandomTexture();
	void Init();
};


float Depth = texture2D(DepthBuffer, gl_TexCoord[0].st).r;

float SSAO = 0.0;

if (Depth < 1.0)
{
vec4 Position = ProjectionBiasInverse * vec4(gl_TexCoord[0].st, Depth, 1.0);
Position.xyz /= Position.w;

vec3 Normal = normalize(texture2D(NormalBuffer, gl_TexCoord[0].st).rgb * 2.0 - 1.0);

vec3 RandomUnitVector = normalize(texture2D(RandomUnitVectorsTexture, gl_TexCoord[1].st).rgb * 2.0 - 1.0);

vec3 Tangent = normalize(RandomUnitVector - Normal * dot(RandomUnitVector, Normal));
vec3 Binormal = cross(Normal, Tangent);

mat3x3 RotationMatrix = mat3x3(Tangent, Binormal, Normal);

for(int i = 0; i < 16; i++)
{
vec3 SamplePosition = RotationMatrix * Samples[i] + Position.xyz;

vec4 SamplePositionProjected = BiasProjection * vec4(SamplePosition, 1.0);
SamplePositionProjected.xyz /= SamplePositionProjected.w;

float SampleDepth = texture2D(DepthBuffer, SamplePositionProjected.st).r;

if(SampleDepth < SamplePositionProjected.z)
{
vec4 SamplePositionUnprojected = ProjectionBiasInverse * vec4(SamplePositionProjected.st, SampleDepth, 1.0);
SamplePositionUnprojected.xyz /= SamplePositionUnprojected.w;

vec3 p2p = SamplePositionUnprojected.xyz - Position.xyz;

float Distance2 = dot(p2p, p2p);

SSAO += 1.0 / (1.0 + Distance2);
}
}

SSAO = 1.0 - SSAO / 16.0;
}

gl_FragColor = vec4(vec3(SSAO), 1.0);
*/